import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
		questCode:200,
		cardList: [],
		shopId: ''
	},
	mutations: {
		setShopId(state, id) {
			state.shopId = id
		},
		setCode(state,code){
			state.questCode = code
		},
		setCard(state, data) {
			let isNow = state.cardList.filter((it) => it.id === data.id && it.ruleType === data.ruleType)
			if(isNow.length !== 0) {
				// 相同类型的产品及规格 直接加数量
				let index = state.cardList.findIndex((it) => it.id === data.id && it.ruleType === data.ruleType)
				state.cardList[index].num = state.cardList[index].num + data.num
				state.cardList[index].totalPrice = state.cardList[index].totalPrice + data.totalPrice
			} else {
				state.cardList.push({
					...data,
					check: false
				})
			}
		},
		updateCard(state, data) {
			state.cardList[data.index].num = data.num
			state.cardList[data.index].totalPrice = data.totalPrice
		},
		updateKey(state, data) {
			state.cardList[data.index][data.key] = data.data
		},
		checkAllCard(state, bool) {
			state.cardList.forEach((it) => {
				it.check = bool
			})
		},
		clearCard(state) {
			state.cardList = []
		},
		delCard(state, index) {
			state.cardList.splice(index, 1)
		}
	},
	actions: {

	}
})

export default store